package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.base.entity.BaseEntity;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.WpUploadLog;
import cn.stylefeng.guns.modular.gridsystem.mapper.WpUploadLogMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.WpUploadLogParam;
import cn.stylefeng.guns.modular.gridsystem.model.result.BizGatewayNewFileResult;
import cn.stylefeng.guns.modular.gridsystem.model.result.WpUploadLogResult;
import cn.stylefeng.guns.modular.gridsystem.service.WpUploadLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 倾角上传日志 服务实现类
 *
 * @author ssy
 * @date 2022/02/16 10:59
 */
@Service
public class WpUploadLogServiceImpl extends ServiceImpl<WpUploadLogMapper, WpUploadLog> implements WpUploadLogService {

    @Override
    public PageResult<WpUploadLogResult> page(WpUploadLogParam wpUploadLogParam) {
           Page<WpUploadLogResult> mapPage = PageFactory.defaultPage();
//        // 构造条件
//        LambdaQueryWrapper<WpUploadLog> queryWrapper = new LambdaQueryWrapper<>();
//        if(wpUploadLogParam!=null&& ObjectUtil.isNotEmpty(wpUploadLogParam.getChannelId())){
//            queryWrapper.eq(WpUploadLog::getChannelId,wpUploadLogParam.getChannelId());
//        }

        String gatewayIds = wpUploadLogParam.getGatewayIds();
        if(StringUtils.isNotBlank(gatewayIds)){
            final String[] split = gatewayIds.split(",");
            mapPage = this.baseMapper.pageList(PageFactory.defaultPage(), wpUploadLogParam, split);
        }

        // 查询分页结果
        return new PageResult<>(mapPage);
    }

    @Override
    public List<WpUploadLog> list(WpUploadLogParam wpUploadLogParam) {

        // 构造条件
        LambdaQueryWrapper<WpUploadLog> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(WpUploadLogParam wpUploadLogParam) {

        // 将dto转为实体
        WpUploadLog wpUploadLog = new WpUploadLog();
        BeanUtil.copyProperties(wpUploadLogParam, wpUploadLog);

        this.save(wpUploadLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(WpUploadLogParam wpUploadLogParam) {
        this.removeById(wpUploadLogParam.getId());
    }

    @Override
    public void edit(WpUploadLogParam wpUploadLogParam) {

        // 根据id查询实体
        WpUploadLog wpUploadLog = this.queryWpUploadLog(wpUploadLogParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(wpUploadLogParam, wpUploadLog);

        this.updateById(wpUploadLog);
    }

    @Override
    public WpUploadLog detail(WpUploadLogParam wpUploadLogParam) {
        return this.queryWpUploadLog(wpUploadLogParam);
    }

    @Override
    public WpUploadLog getNewOne(WpUploadLogParam wpUploadLogParam) {
        QueryWrapper<WpUploadLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("channel_id",wpUploadLogParam.getChannelId());
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }


    /**
     * 获取倾角上传日志
     *
     * @author ssy
     * @since 2022/02/16 10:59
     */
    private WpUploadLog queryWpUploadLog(WpUploadLogParam wpUploadLogParam) {
        WpUploadLog wpUploadLog = this.getById(wpUploadLogParam.getId());
        return wpUploadLog;
    }
}
